//
//  NewNameOrCreaterFloderViewController.m
//  OfficeEtc
//
//  Created by  on 12-7-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NewNameOrCreaterFloderViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "FileTableViewController.h"
#import "FileTableViewController.h"
#import "AppDelegate.h"
#import "RootViewController.h"
#import "AudioMainViewController.h"
@interface NewNameOrCreaterFloderViewController ()

@end

@implementation NewNameOrCreaterFloderViewController
@synthesize m_OperMode;
@synthesize m_OperPath;

-(void) viewDidLoad
{
    [self.view setBackgroundColor:[UIColor darkGrayColor]];
    
    UILabel* tAlertLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 300, 34)];
    [tAlertLabel setBackgroundColor:[UIColor clearColor]];
    [tAlertLabel setFont:[UIFont boldSystemFontOfSize:16.f]];
    [tAlertLabel setTextAlignment:UITextAlignmentCenter];
    [self.view addSubview:tAlertLabel];
    [tAlertLabel release];
    
    p_InputName = [[UITextField alloc] initWithFrame:CGRectMake(18, 100, 282, 30)];
    p_InputName.delegate =self;
    [p_InputName setFont:[UIFont systemFontOfSize:22]];
    [p_InputName setBackgroundColor:[UIColor clearColor]];
    [p_InputName setClipsToBounds:NO];
    CALayer* tBackLayer = [CALayer layer];
    tBackLayer.frame = CGRectMake(-4, -2, 290, 30);
    [tBackLayer setBackgroundColor:[UIColor whiteColor].CGColor];
    tBackLayer.cornerRadius =4.f;
    tBackLayer.shadowColor = [UIColor blackColor].CGColor;
    tBackLayer.shadowOffset =CGSizeMake(3, 3);
    tBackLayer.shadowOpacity = 1.0f;
    tBackLayer.masksToBounds =YES;
    [p_InputName.layer addSublayer:tBackLayer];
    p_InputName.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:p_InputName];
    [p_InputName becomeFirstResponder];
    if (m_OperMode == OperMode_ReName) {
        self.navigationItem.title = @"重命名";
        [tAlertLabel setText:@"请输入该文件新的名称"];
    }else if(m_OperMode == OperMode_CreateFloder){
        self.navigationItem.title = @"新建文件夹";
        [tAlertLabel setText:@"请输新创建的文件夹的名称"];
    }
}

-(void) dealloc
{
    [m_OperPath release];
    [p_InputName release];
    [super dealloc];
}
#pragma mark-
#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSFileManager* tFileManager = [NSFileManager defaultManager];
    NSError* tError = nil;
    if (m_OperMode == OperMode_ReName) {
        NSString* tNewPath = [m_OperPath stringByDeletingLastPathComponent];
        NSString* tExtenStr = [m_OperPath pathExtension];
        NSString* tTNew =nil;
        if ([tExtenStr length] > 0) {
            tTNew = [tNewPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",textField.text,tExtenStr]];
        }else {
            tTNew =[tNewPath stringByAppendingPathComponent:textField.text];
        }

        if ([tFileManager fileExistsAtPath:tTNew]) {
            UIAlertView* tAlert = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"当前路径下已存在同名文件" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
            [tAlert show];
            [tAlert release];
            return YES;
        }
        NSError* tError = nil;
        BOOL tRenameOk= [tFileManager moveItemAtPath:m_OperPath toPath:tTNew error:&tError];
        if (!tRenameOk) {
            UIAlertView* tAlert = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"重命名文件失败！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
            [tAlert show];
            [tAlert release];
            NSLog(@"rename file item error =%@",[tError description]);
            return YES;
        }
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
        UINavigationController* tRootNav = (UINavigationController*)tAppDel.window.rootViewController;
        RootViewController* tRootVC = (RootViewController*)[tRootNav.viewControllers objectAtIndex:0];
        if ([m_OperPath rangeOfString:tRootVC.m_AudioVC.m_MusicPath].location != NSNotFound) {
            [tRootVC.m_AudioVC reNameFileByFTP:tTNew oldName:m_OperPath];
        }
    }else if(m_OperMode == OperMode_CreateFloder){
        NSString* tNewFloder = [m_OperPath stringByAppendingPathComponent:textField.text];
        BOOL tOperOk =[tFileManager createDirectoryAtPath:tNewFloder withIntermediateDirectories:YES attributes:nil error:&tError];
        if (!tOperOk) {
            NSLog(@"create floder error =%@",[tError description]);
            UIAlertView* tAlert = [[UIAlertView alloc] initWithTitle:@"错误提示" message:@"创建新的文件夹失败！" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
            [tAlert show];
            [tAlert release];
            return YES;
        }
    }
    FileTableViewController* tSuperFTVC = [self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count]-2];
    [tSuperFTVC.m_EditItems removeAllObjects];
    [self.navigationController popViewControllerAnimated:YES];
    
    [tSuperFTVC flashItemsToShow];
    return YES;
}
@end
